/**
 * 微信成交统计
 * - 使用自定义封装组件，简化代码，统一管理。
 * - 2025-08-05 15:04:49 tzj
 * */

import {mrhzApi, zdfpApi, zdfpApi as selectPage} from "../../services/basic";
import Dictionary from "../../services/dictionary";
import {formatNumber} from "../../utils/math";
// import {Select} from "../../components/Select";
import React from "react";
import withBasePage from "../../components/BaseComponent/withBasePage";
import CompanyGroup from "../../components/CompanyGroup";

// 要显示的form表单
const formItemList =
  [
    "companies",
    {
      name: 'serviceWx2',
      keyword: 'lxr',
      label: "销售"
    },
    "check",
    // "zwSelect",
    {name: "chooseDateRange", label: '成交日期'}
  ];

// 显示的按钮
const buttons = ["query", "export",];
// 表格列信息
const columns = [
  {
    title: '销售导购',
    dataIndex: 'xm',
    sumName: "合计",
    sorter: true
  },
  {
    title: '到店未成交',
    dataIndex: 'ddwcj',
    sorter: true
  },
  {
    title: '到店已成交',
    dataIndex: 'ddycj',
    sorter: true
  },
  {
    title: '异公司到店',
    dataIndex: 'ygsdd',
    sorter: true
  },
  {
    title: '异公司已成交',
    dataIndex: 'ygscj',
    sorter: true
  },
  {
    title: '到店总数量',
    dataIndex: 'zdd',
    tip: "到店已成交+到店未成交+异公司到店已成交+异公司到店未成交",
    // 已成交、未成交、异公司全算
    render(_, record) {
      return Number(record["ddwcj"]) + Number(record["ddycj"]) + Number(record["ygsdd"]) + Number(record["ygscj"])
    },
    sorter: true,
    formula: "=SUM({{ddwcj}},{{ddycj}},{{ygsdd}},{{ygscj}})"
  },
  {
    title: '有效留资',
    dataIndex: 'yxlz',
    sorter: true
  },
  {
    title: '到店比',
    tip: '到店比=有效留资数/总到店数',
    dataIndex: 'ddb',
    render: (_, record) => {
      const zdd = Number(record.ddwcj) + Number(record.ddycj) + Number(record.ygscj) + Number(record.ygsdd)
      if (!zdd) return 0
      return (Number(record.yxlz) / Number(zdd)).toFixed(2)
    },
    numFmt: "0.00",
    formula: "=IFERROR({{yxlz}}/{{zdd}}, 0)",
    sorter: true
  },
  {
    title: '成交总数量',
    dataIndex: 'cjSum',
    render(_, record) {
      return Number(record["ddycj"]) + Number(record["ygscj"])
    },
    sorter: true,
    formula: "=SUM({{ddycj}},{{ygscj}})"
  },
  {
    title: '未成交总数',
    dataIndex: 'wcjSum',
    render(_, record) {
      return Number(record["ddwcj"]) + Number(record["ygsdd"])
    },
    sorter: true,
    formula: "=SUM({{ddwcj}},{{ygsdd}})"
  },
  {
    title: '成交金额',
    dataIndex: 'cjje',
    sorter: true
  },
  {
    title: '成交率(%)',
    tip: '成交总数量 ÷ 到店总数量 * 100%',
    dataIndex: 'wxcjl',
    sorter: true,
    numFmt: "0.00",
    formula: "=IFERROR({{cjSum}}/{{zdd}}, 0)",
    render: (_, record) => {
      const zdd = Number(record["ddwcj"]) + Number(record["ddycj"]) + Number(record["ygsdd"]) + Number(record["ygscj"])
      if (!zdd) return 0
      const cjSum = Number(record["ddycj"]) + Number(record["ygscj"])
      return formatNumber(cjSum / zdd * 100)
    }
  },
  {
    title: '客单价',
    tip: '客单价 = 成交金额 ÷ 成交总数量',
    formula: "=IFERROR({{cjje}}/{{cjSum}}, 0)",
    dataIndex: 'wxkdj',
    sorter: true,
    render: (_, record) => {
      const cjSum = Number(record["ddycj"]) + Number(record["ygscj"])
      if (!cjSum) return 0
      return formatNumber(Number(record.cjje) / cjSum)
    }
  },
  {
    title: '到店间隔天数',
    tip: "留资日期与客户到店日期的间隔天数",
    dataIndex: 'ddpjcz',
    sumName: "",
    sorter: true
  },
]

// 实际执行还是在class里，而非视为function
const modalRef = React.createRef()

function EditBtn({record, hoc}) {
  const groupClick = () => {
    modalRef.current.showModal(record, hoc.form.dataForm)
  }
  return (<span style={{fontSize: '14px', color: '#10b6cc'}} onClick={groupClick}>公司分组</span>)
}

export default withBasePage({
  state: {
    formItemList,
    buttons,
    // columns,
    filename: "销售导购成交统计",
    queryAll: true,
    sumRow: true,
    editOther: [
      EditBtn
    ],
    params: {
      zw: 2
    }
  },
  columns,
  queryApi: mrhzApi.selectPagewxcj,
  hasLzModal: true,
  onRowDoubleClick: (record) => {
    const params = {
      ...this.form.dataForm,
      yybm: record.yybm,
      lxr: record.gh,
      // jz: 1
    }
    const config = {
      queryApi: mrhzApi.selectPagecj,
      columns: Dictionary.dealColumns,
      tableTitle: "成交详情",
      // width: 1200
    }
    this.lzModalRef.open(params, config);
  },
  extraContentAfterTable: (hoc) => <CompanyGroup editItemDone={() => hoc.queryDataList()} ref={modalRef}/>,
})(() => null);
